﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OracleClient;
using DBUtility;
using System.Data;

namespace TierApplication
{    
    public partial class P_User
    {
        public UserInfo mUserInfo;
        public ArrayList permissionList;
        public ArrayList roleList;

        public P_User()
        {
        }

        public P_User(int userID)
        {
            mUserInfo = GetUserInfo(userID.ToString());
            this.permissionList = GetEffectivePermissionList(mUserInfo.UserId.ToString());
        }

        public P_User(string usercode)
        {
            mUserInfo = GetUserInfoByUserCode(usercode);
            this.permissionList = GetEffectivePermissionList(mUserInfo.UserId.ToString());
        }

        public bool HasPermission(string sMoudleID, Int32 iPowerValue)
        {
            bool bResult = false;
            for (int i = 0; i < permissionList.Count; i++)
            {
                string sPermission = permissionList[i].ToString();
                string[] sModulePower = sPermission.Split(',');
                string modleid = sModulePower[0];
                string powervalue = sModulePower[1];
                Int32 ipowervalue = Int32.Parse(powervalue);
                if (modleid == sMoudleID)
                {
                    if ((ipowervalue & iPowerValue) != 0)
                    {
                        bResult = true;
                    }
                    break;
                }
            }
            return bResult;
        }


        public bool IsInRole(string role)
        {
            return this.roleList.Contains(role);
        }


        public ArrayList Permissions
        {
            get
            {
                return this.permissionList;
            }
        }


        public ArrayList Roles
        {
            get
            {
                return this.roleList;
            }
        }

        public ArrayList GetEffectivePermissionList(string userID)
        {
            ArrayList list = new ArrayList();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT  DISTINCT USERID, MOUDLEID,POWERVALUE ");
            strSql.Append(" FROM  P_USERROLE A, P_ROLEMOUDLE B ");
            strSql.Append(" where A.ROLEID = B.ROLEID AND A.USERID=:USERID ");
            OracleParameter[] parameters = {
					new OracleParameter(":USERID", OracleType.Int32,4)};
            parameters[0].Value = int.Parse(userID);

            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string sMOUDLEID = ds.Tables[0].Rows[i]["MOUDLEID"].ToString();
                string sPOWERVALUE = ds.Tables[0].Rows[i]["POWERVALUE"].ToString();
                list.Add(sMOUDLEID + "," + sPOWERVALUE);
            }
            return list;
        }

        public ArrayList GetUserRoles(int userID)
        {
            ArrayList list = new ArrayList();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT * ");
            strSql.Append(" FROM P_USERROLE ");
            strSql.Append(" where USERID=:USERID ");
            OracleParameter[] parameters = {
					new OracleParameter(":USERID", OracleType.Int32,4)};
            parameters[0].Value = userID;

            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(ds.Tables[0].Rows[i]["ROLEID"].ToString());
            }
            return list;
        }

    }
}